﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SPA_Prototype.Model;

namespace SPA_Prototype
{
    public partial class UserManagement : Form
    {
        private USER_TYPE _User_Type;
        private AdminManagement _AdmM;
        private EmailManagement _EmaM;
        private BackupManagement _BacM;
        private VIPManagement _VIPM;

        public UserManagement(USER_TYPE us,AdminManagement am, BackupManagement bm, VIPManagement vipm)
        {
            InitializeComponent();
            _User_Type = us;
            _AdmM = am;
            _BacM = bm;
            _VIPM = vipm;
        }

        private void UserManagement_Load(object sender, EventArgs e)
        {
            

            if ((_User_Type == USER_TYPE.ADMIN) || (_User_Type == USER_TYPE.ENGINEER))
            {
                // tab users
                GbNewUser.Enabled = true;
                PbDelete.Enabled = true;

                // tab email
                TabEmail_GroupBox.Enabled = true;

                // tab Backup
                TabBackup_Panel.Enabled = true;

                // tab VIP
                TabVIP_Account_Txt.Text = _VIPM.MinAccount.ToString();
                TabVIP_Visit_Txt.Text = _VIPM.MinVisitTimes.ToString();
            }
            else  // MOD user
            {
                // tab users
                GbNewUser.Enabled = false;
                PbDelete.Enabled = false;

                // tab email
                TabEmail_GroupBox.Enabled = false;

                // tab Backup
                TabBackup_Panel.Enabled = false;

                // tab VIP
                TabVIP_Account_Txt.Text = _VIPM.MinAccount.ToString();
                TabVIP_Visit_Txt.Text = _VIPM.MinVisitTimes.ToString();
            }

    /*        if (_User_Type == USER_TYPE.MOD)
            {
                // tab email
                TabEmail_GroupBox.Enabled = true;

                // tab Backup
                TabBackup_Panel.Enabled = true;

                // tab VIP
                TabVIP_Account_Txt.Text = _VIPM.MinAccount.ToString();
                TabVIP_Visit_Txt.Text = _VIPM.MinVisitTimes.ToString();
            }*/


            UpdateUser();

            // update informations for Email tab
            _EmaM = new EmailManagement();
            TxtEmail.Text = _EmaM.EmailAdress;

            // update informations for Backup/Restore tab 
            TabBackupInformationsFilling();
        }
    
        private void UpdateUser()
        {
            _AdmM.GetUserList(_User_Type);
            CbCP_User.Items.Clear();
            int count = _AdmM.User.Count;
            if ((_User_Type == USER_TYPE.NORMAL_USER) || (_User_Type == USER_TYPE.MOD) )
            {
                CbCP_User.Items.Add(_AdmM.Current_User);
            }
            else  // engineer user
                for (int j = 0; j < count; j++) CbCP_User.Items.Add(_AdmM.User[j]);

            TxtNU_User.Clear();
            TxtNU_Pass.Clear();
            TxtCP_Confirm.Clear();
            TxtCP_NewPass.Clear();
            CbCP_User.Text = "";
        }

        private void Button_Click(object sender, EventArgs e)
        {
            if (sender == PbAdd)
            {
                _AdmM.AddNewUser(TxtNU_User.Text, TxtNU_Pass.Text);              
                MessageBox.Show("Successful", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                UpdateUser();
            }
            if (sender == PbUpdate)
            {
                if (TxtCP_NewPass.Text != TxtCP_Confirm.Text)
                {
                    MessageBox.Show("Password confirmed does not match with New Password", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    TxtCP_NewPass.Clear();
                    TxtCP_Confirm.Clear();
                    return;
                }

               bool bSuccess = _AdmM.UpdatePass(CbCP_User.Text, TxtCP_NewPass.Text);
               UpdateUser();

               if (bSuccess == true) MessageBox.Show("Successful", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
               else MessageBox.Show("Error ! Try Again", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
               TxtCP_NewPass.Clear();
               TxtCP_Confirm.Clear();
            }
            if (sender == PbDelete)
            {
                if ((CbCP_User.Text == CbCP_User.Items[0].ToString()))
                {
                    MessageBox.Show("Can not delete this user", "WARNING", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
                    return;
                }
                var m = MessageBox.Show("Do you want to delete this user", "INFO", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (m == System.Windows.Forms.DialogResult.Yes)
                {
                   bool bSuccess = _AdmM.DeleteUser(CbCP_User.Text);
                   if (bSuccess == true) MessageBox.Show("Successful", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);                 
                    else MessageBox.Show("Error ! Try Again", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);       

                }
                UpdateUser();
            }
        }

        private void PbEmailUpdate_Click(object sender, EventArgs e)
        {    
            _EmaM.EmailAdress = TxtEmail.Text;
        }

        private void TabBackupInformationsFilling()
        {
            TabBackup_Auto_Rd.Checked = _BacM.Auto;
            TabBackup_Manual_Rd.Checked = !_BacM.Auto;
            TabBackup_Email_Txt.Text = _BacM.EmailAddr;
            TabBackup_Folder_Txt.Text = _BacM.Folder;
            TabBackup_Period_Num.Value = _BacM.Period;
        }
        private void TabChanged(object sender, EventArgs e)
        {
            switch (TabControl.SelectedIndex)
            {
                case 0:         // Users tab
                    break;
                case 1:         // Email tab
                    TxtEmail.Text = _EmaM.EmailAdress;
                    break;
                case 2:         // Back up tab
                    TabBackupInformationsFilling();
                    break;
            }

        }

        private void TabBackup_Auto_Rd_CheckedChanged(object sender, EventArgs e)
        {
            if (TabBackup_Auto_Rd.Checked) TabBackup_GroupBox.Enabled = true;
            else TabBackup_GroupBox.Enabled = false;
        }

        private void TabBackup_ButtonClick(object sender, EventArgs e)
        {
            if (sender == TabBackup_Browse_Pb)
            {
                FolderBrowserDialog browser = new FolderBrowserDialog();
                var m = browser.ShowDialog();
                if (m == System.Windows.Forms.DialogResult.OK)
                {
                    TabBackup_Folder_Txt.Text = browser.SelectedPath;
                }
            }
            if (sender == TabBackup_Update_Pb)
            {
                if ((TabBackup_Auto_Rd.Checked) && (TabBackup_Email_Txt.Text == "") && (TabBackup_Folder_Txt.Text == ""))
                {
                    MessageBox.Show("Invalid !", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    return;
                }
                _BacM.Update(TabBackup_Auto_Rd.Checked, TabBackup_Email_Txt.Text, TabBackup_Folder_Txt.Text,(int) TabBackup_Period_Num.Value);
                
            }
        }

        private void TabVIP_PbClick(object sender, EventArgs e)
        {
            if (sender == TabVIP_Update_Pb)
            {
                int temp = 0;
                int.TryParse(TabVIP_Account_Txt.Text, out temp);
                _VIPM.MinAccount = temp;

                temp = 0;
                int.TryParse(TabVIP_Visit_Txt.Text, out temp);
                _VIPM.MinVisitTimes = temp;

                _VIPM.UpdateDB();
            }
        }

       
    }
}
